﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NCT.Framework;
using VISS.API.Users;

namespace VISS.Web.Frontend.Modules.User.Profile {
    public partial class AlbumPage : System.Web.UI.Page {

        protected int CurrentPage;

        protected void Page_Load(object sender, EventArgs e) {
            UserContext userContext = LoginFactory.GetCurrentUser();

            string userKey = ConvertUtils.ToString(Request.QueryString["key"]);
            int id = ConvertUtils.ToInt(Request.QueryString["id"], -1);
            CurrentPage = ConvertUtils.ToInt(Request.QueryString["page"], 1);

            if (userContext == null) {
                Response.Redirect("/login?url=" + AppSettings.RootURL + "profile/album?u=" + userKey + "&k=" + id + "&p=" + CurrentPage);
                return;
            }
            
            int totalImage = 0;

            UserImageEnt selectedImage = null;
            List<UserImageEnt> imageList = null;
            IUserImage _objectImage = StructureMapFactory.GetInstance<IUserImage>();
            if (userKey.Length == 0) {
                imageList = _objectImage.GetByUserKeyWithPaging(userContext.UserKey, CurrentPage, AppSettings.PageSizeOfImage, out totalImage);
                if (imageList.Count > 0) {
                    if (id > 0) {
                        for (int i = 0; i < imageList.Count; i++) {
                            if (id == imageList[i].ID) {
                                selectedImage = imageList[i];
                                break;
                            }
                        }
                    }
                    else {
                        selectedImage = imageList[0];
                    }
                }
                idImageListBox.IsPersonal = true;
                idImageBox.IsPersonal = true;
            }
            else {
                if (userKey != userContext.UserKey && (userContext.Status & EUserStatus.Verified) != EUserStatus.Verified) {
                    Response.Redirect("/error?t=" + (int)EErrorType.AccessDenied);
                    return;
                }

                IUserGeneralProfile _objectGeneralProfile = StructureMapFactory.GetInstance<IUserGeneralProfile>();
                UserGeneralProfileEnt generalProfileEnt = _objectGeneralProfile.GetByUserKey(userKey);
                if (generalProfileEnt == null) {
                    Response.Redirect("/error?t=" + (int)EErrorType.Http404);
                    return;
                }
                imageList = _objectImage.GetByUserKeyWithPaging(userKey, CurrentPage, AppSettings.PageSizeOfImage, out totalImage);
                int index = -1;
                if (imageList.Count > 0) {
                    if (id > 0) {
                        for (int i = 0; i < imageList.Count; i++) {
                            if (id == imageList[i].ID) {
                                selectedImage = imageList[i];
                                index = i;
                                break;
                            }
                        }
                    }
                    else {
                        selectedImage = imageList[0];
                        index = 0;
                    }
                }
                idImageListBox.IsPersonal = false;
                idImageListBox.UserName = generalProfileEnt.Name;
                idImageListBox.UserKey = userKey;
                idImageBox.UserKey = userKey;
                if (index < imageList.Count - 1) {
                    idImageBox.NextImageID = imageList[index + 1].ID;
                    idImageBox.PageNum = CurrentPage;
                }
                else {
                    int totalPage = totalImage / AppSettings.PageSizeOfImage;
                    if (totalImage % AppSettings.PageSizeOfImage != 0) {
                        totalPage++;
                    }
                    if (CurrentPage < totalPage) {
                        idImageBox.PageNum = CurrentPage + 1;
                    }
                    else {
                        idImageBox.PageNum = -1;
                    }
                    idImageBox.NextImageID = -1;
                }
            }

            idImageBox.SelectedImage = selectedImage;
            idImageListBox.ImageList = imageList;
            idImageListBox.ImageCount = totalImage;
            idImageListBox.ViewPage = EImageListBoxViewPage.AlbumPage;

            // SEO
            IHeadSeo seo = this.Master as IHeadSeo;
            seo.Title = StringResources.SEO.TitleUserAlbum + " | " + userContext.FullName + " | " + StringResources.SEO.TitleDefault;
            seo.Description = StringResources.SEO.DescriptionDefault;
            seo.Keywords = StringResources.SEO.KeywordsDefault;
        }
    }
}
